package com.longjianghuasheng.logistics.business.home.adapter

import android.view.View
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.business.home.HomeBean
import com.longjianghuasheng.logistics.business.home.TYPE_BANNER
import com.longjianghuasheng.logistics.business.home.TYPE_NEWS
import com.longjianghuasheng.logistics.business.home.TYPE_TITLE
import com.longjianghuasheng.logistics.ext.initGridDecoration
import com.longjianghuasheng.logistics.ext.initLinerDecoration
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.xuexiang.xui.widget.banner.widget.banner.SimpleImageBanner
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding

class HomeAdapter : BaseMultiItemQuickAdapter<HomeBean, BaseViewHolder>() {

    var isFirst = true


    var onNewsListener = { id: Int ->

    }

    var onTabListener = { id: Int ->

    }

    var onServiceListener = { view: View ->

    }

    init {
        addItemType(TYPE_TITLE, R.layout.home_title)
        addItemType(TYPE_BANNER, R.layout.home_banner)
        addItemType(TYPE_NEWS, R.layout.home_news)
    }

    val newsAdapter by lazy {
        NewsAdapter()
    }

    override fun convert(holder: BaseViewHolder, item: HomeBean) {
        holder.absoluteAdapterPosition
        when (item.itemType) {
            TYPE_TITLE -> {
                holder.setText(R.id.tv_city, item.city)
                if (isFirst) {
                    isFirst = false
                    holder.getView<ConstraintLayout>(R.id.cl_bg).addStatusBarTopPadding()
                }

            }
            TYPE_BANNER -> initBanner(holder, item)
            TYPE_NEWS -> initNews(holder, item)
            else -> {}
        }

    }

    private fun initBanner(holder: BaseViewHolder, item: HomeBean) {

        holder.getView<ImageView>(R.id.iv_service).setOnClickListener(onServiceListener)

        val banner = holder.getView<SimpleImageBanner>(R.id.banner)


        banner.setSource(item.newsResponse.banner?.map {

            BannerItem().apply {
                imgUrl = it
            }

        })
//            .setSelectAnimClass(
//                ZoomInEnter::class.java
//            )
//            .setTransformerClass(ZoomOutSlideTransformer::class.java)
            .setOnItemClickListener { view: View?, t: BannerItem?, position: Int -> }
            .setIsOnePageLoop(false).startScroll()


        val rvTab = holder.getView<RecyclerView>(R.id.rv_tab)
        val tabAdapter = TabAdapter()
        tabAdapter.setOnItemClickListener { _, _, position ->

            onTabListener.invoke(tabAdapter.data[position].id ?: 0)

        }

        rvTab.initGridDecoration(
            spanCount = 4,
            space = 0F,
            headFootSpace = 0F,
            adapter = tabAdapter
        )
        tabAdapter.setNewInstance(item.newsResponse.type)

    }

    private fun initNews(holder: BaseViewHolder, item: HomeBean) {

        val rvNews = holder.getView<RecyclerView>(R.id.rv_news)

        newsAdapter.setOnItemClickListener { adapter, view, position ->
            onNewsListener.invoke(newsAdapter.data[position].id ?: 0)

        }

        rvNews.initLinerDecoration(adapter = newsAdapter).setHasFixedSize(false)

        newsAdapter.setNewInstance(item.newsResponse.data)

    }
}